import functools

from flask import session, current_app, g

from info.models import User


# 自定义过滤器
def do_news_list(index):
    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"
    else:
        return ""


# 自定义装饰器，用于访问视图函数前，查询用户登录状态
def user_login_data(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # 1、获取用户id
        user_id = session.get("user_id")
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return func(*args, **kwargs)

    return wrapper
